<template>
  <div class="login">
    <div class="main">
      <img src="@/assets/image/patrol/login/left.png" alt="" class="img2">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
        <h2 class="title">校园安全风险<br></br>防控数字化改造</h2>
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" type="text" auto-complete="off" @focus="isUsername=true"
            @blur="isUsername=false" placeholder="用户名/手机号">
            <svg-icon slot="prefix" icon-class="user" v-bind:class="{'color-class':isUsername}" class=" input-icon " />
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码"
            @focus="isPassword=true" @blur="isPassword=false" @keyup.enter.native="handleLogin">
            <svg-icon slot="prefix" icon-class="password" v-bind:class="{'color-class':isPassword}"
              class="el-input__icon input-icon " />
          </el-input>
        </el-form-item>
        <!--        <el-form-item prop="code">-->
        <!--          <el-input-->
        <!--            v-model="loginForm.code"-->
        <!--            auto-complete="off"-->
        <!--            placeholder="验证码"-->
        <!--            @focus="isValidCode=true"-->
        <!--            @blur="isValidCode=false"-->
        <!--            @keyup.enter.native="handleLogin"-->
        <!--          >-->
        <!--            <svg-icon slot="prefix" icon-class="validCode" v-bind:class="{'color-class':isValidCode}" class="el-input__icon input-icon "  />-->
        <!--          </el-input>-->
        <!--          <div class="login-code">-->
        <!--            <img :src="codeUrl" @click="getCode" />-->
        <!--          </div>-->
        <!--        </el-form-item>-->
        <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
        <a style="color: #1a91ff;float: right;font-size: 14px;">
          <img :src="qurl" alt="" id="img" width="100" height="100" style="vertical-align: middle" />
          APP下载
        </a>
        <el-form-item style="width:100%;">
          <el-button :loading="loading" size="medium" type="primary" style="width:100%;"
            @click.native.prevent="handleLogin">
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
        </el-form-item>
        <span @click="download" style="color: #0075C2;font-size: 12px;cursor: pointer;">点此下载操作手册</span>
      </el-form>
    </div>

    <!--  底部  -->
    <!--    <div class="el-login-footer">-->
    <!--      <span>© 2020 All Rights Reserved. ruoyichina.com<br> 蒙ICP备2020003903号 <img src="" alt=""></span>-->
    <!--    </div>-->
  </div>
</template>

<script>
  import {
    getCodeImg,
    QR
  } from '@/api/login'
  import Cookies from "js-cookie";
  import {
    encrypt,
    decrypt
  } from '@/utils/jsencrypt'

  export default {
    name: "Login",
    data() {
      let validateUsername = (rule, value, callback) => {
        if (value === '') {
          this.loginForm.password = "";
        }
        callback();
      };
      return {
        isUsername: false,
        isPassword: false,
        isValidCode: false,
        codeUrl: "",
        cookiePassword: "",
        loginForm: {
          username: "admin",
          password: "JBxyxc!@#123",
          rememberMe: false,
          code: "",
          uuid: ""
        },
        loginRules: {
          username: [{
              required: true,
              trigger: "blur",
              message: "用户名不能为空"
            },
            {
              validator: validateUsername,
              trigger: 'change'
            }
          ],
          password: [{
            required: true,
            trigger: "blur",
            message: "密码不能为空"
          }],
          // code: [{ required: true, trigger: "change", message: "验证码不能为空" }]
        },
        loading: false,
        redirect: undefined,
        qurl: ""
      };
    },
    watch: {
      $route: {
        handler: function (route) {
          this.redirect = route.query && route.query.redirect;
        },
        immediate: true
      }
    },
    created() {
      // this.getCode();
      this.getCookie();
      this.getQR();
    },
    methods: {
      getQR() {
        QR().then(res => {
          this.qurl = "data:image/gif;base64," + res;
        })
      },
      getCode() {
        getCodeImg().then(res => {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        });
      },
      getCookie() {
        const username = Cookies.get("username");
        const password = Cookies.get("password");
        const rememberMe = Cookies.get('rememberMe')
        this.loginForm = {
          username: username === undefined ? this.loginForm.username : username,
          password: password === undefined ? this.loginForm.password : decrypt(password),
          rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
        };
      },
      handleLogin() {
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.loading = true;
            if (this.loginForm.rememberMe) {
              Cookies.set("username", this.loginForm.username, {
                expires: 30
              });
              Cookies.set("password", encrypt(this.loginForm.password), {
                expires: 30
              });
              Cookies.set('rememberMe', this.loginForm.rememberMe, {
                expires: 30
              });
            } else {
              Cookies.remove("username");
              Cookies.remove("password");
              Cookies.remove('rememberMe');
            }
            this.$store
              .dispatch("Login", this.loginForm)
              .then((type) => {
                this.$store.dispatch("Set_Type", type);
                switch (type) {
                  case "guard":
                    this.$alert("安保人员不需要登录后台系统，请在手机登录", "提示", {
                      dangerouslyUseHTMLString: true
                    });
                    this.loading = false;
                    this.$store.dispatch('LogOut');
                    break;
                  case "staff":
                    this.$alert("行政人员不需要登录后台系统，请在手机登录", "提示", {
                      dangerouslyUseHTMLString: true
                    });
                    this.loading = false;
                    this.$store.dispatch('LogOut');
                    break;
                  case "thridRole":
                    this.$alert("第三方公司不需要登录后台系统，请在手机登录", "提示", {
                      dangerouslyUseHTMLString: true
                    });
                    this.loading = false;
                    this.$store.dispatch('LogOut');
                    break;
                  case "district":
                  case "admin":
                  case "school":
                    // this.$tab.openPage("行政人员主页", "/patrol/home/school");
                    this.$router.push({
                      path: "/patrol/home"
                    });
                    break;
                }
              })
              .catch(() => {
                this.loading = false;
                // this.getCode();
              });
          }
        });
      },
      download() {
        const a = document.createElement('a')
        a.setAttribute('download', "江北区校园安全巡查督查系统学校管理员和移动端操作手册.docx")
        a.setAttribute('target', '_blank')
        a.setAttribute('href', "http://47.109.80.156:39001/profile/upload/operation/江北区校园安全巡查督查系统学校管理员和移动端操作手册.docx")
        a.click()
      }
    }
  };

</script>

<style rel="stylesheet/scss" lang="scss">
  .login {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    background: url("../assets/image/patrol/login/login_background.png") no-repeat 0px 0px;
    ;
    position: relative;
  }

  .main {
    position: relative;
    width: 940px;
    height: 495px;
    background: rgb(237, 253, 255);
    border-radius: 12px;
    box-shadow: 0px 2px 12px 0px rgba(9, 23, 39, 1);
    color: rgba(255, 255, 255, 1);

    .img1 {
      position: absolute;
      top: 46px;
      left: 53px;
      width: 110px;
    }

    .img2 {
      position: absolute;
      top: 90px;
      left: 30px;
      width: 450px;
      height: 300px;
    }

    h3 {
      margin-left: 52px;
      margin-top: 383px;
      margin-bottom: 0;
      font-size: 32px;

    }

    h4 {
      margin-left: 52px;
      font-size: 24px;
      margin-top: 16px;
    }

    .login-form {
      position: absolute;
      right: 0;
      top: 0;
      border-radius: 6px;
      background: #ffffff;
      width: 450px;
      height: 495px;
      padding: 58px 33px 33px 33px;

      .title {
        width: 300px;
        font-size: 30px;
        margin: 0px auto 60px auto;
        text-align: center;
        color: #0075C2;
        line-height: 20px;
      }

      a img {
        display: none;
      }

      a:hover img {
        z-index: 10;
        position: absolute;
        right: 100px;
        bottom: 160px;
        display: block;
      }

      .el-input {
        height: 32px;

        input:focus {
          color: rgba(6, 104, 185, 1);
        }

        input {
          outline: none;
          height: 38px;
          border-top: 0;
          border-left: 0;
          border-right: 0;
          border-radius: 0;
        }

      }

      .color-class {
        color: rgba(6, 104, 185, 1);
      }

      .input-icon {
        height: 39px;
        width: 19px;
        margin-left: 2px;
      }
    }
  }


  .login-code {
    position: absolute;
    width: 88px;
    height: 32px;
    right: 0;
    bottom: 8px;

    img {
      width: 88px;
      height: 32px;
      cursor: pointer;
      vertical-align: middle;
    }
  }

  .el-login-footer {
    height: 40px;
    line-height: 40px;
    position: absolute;
    transform: translateY(273px);
    width: 100%;
    text-align: center;
    color: #fff;
    font-family: Arial;
    font-size: 12px;
    letter-spacing: 1px;

    span {
      img {
        height: 12px;
      }
    }
  }

</style>
